package com.yanxu.threadandobjectmethods.join;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2025/3/1-2025
 * @Description: join 方法遇到中断
 *  join 方法在遇到中断的时候，是谁中断。
 */
public class JoinInterrput {
    public static void main(String[] args) {
        //主线程引用
        final Thread mainThread = Thread.currentThread();

        //定义子线程
        Thread thread1 = new Thread(new Runnable() {
            public void run() {
                try {
                    System.out.println("子线程正在运行!!!");
                    mainThread.interrupt();
                    Thread.sleep(5000);
                    System.out.println("子线程运行结束");
                }catch (InterruptedException e){
                    e.printStackTrace();
                    System.out.println("子线程被中断了!!!");
                }
            }
        });
        thread1.start();

        try {
            thread1.join();
            System.out.println("主线程运行结束了!!!");
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println(Thread.currentThread().getName()+" 遇到了中断!!!");
            thread1.interrupt();
        }

    }
}